package idv.arthur.work;

import java.io.IOException;
import java.security.acl.Group;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

/**
 * Servlet implementation class GetWorkList
 */
//@WebServlet(description = "取得該帳號所能使用的功能選單", urlPatterns = { "/GetWorkList" })
public class GetWorkList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetWorkList() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Widgets wid = new Widgets();
		
		HttpSession session = request.getSession(false);
		String userId = (String) session.getAttribute("userId");
		
		String workListResult = "";
		
		String strSQL = "SELECT t3.typeSN,t3.name typeName, t2.name itemName, t2.webUrl FROM tms_authorization_body t1, tms_authorization_desc t2, tms_authorization_head t3 WHERE t1.userId=? AND t2.detailSN=t1.detailSN AND t1.typeSN=t3.typeSN ORDER BY typeName DESC";
		ArrayList<WorkList> workLists = new ArrayList<WorkList>();
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			List<Group> Groups = new ArrayList<Group>();
			for (Group group: Groups){
				WorkList workList = new WorkList();
				workList.setTypeName("簽核流程");
				workList.setItemName(group.getName());
				workList.setWebUrl("http://tw.yahoo.com");
				workLists.add(workList);
			}
			
			
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, userId);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				WorkList workList = new WorkList();
				workList.setTypeSN(rs.getInt("typeSN"));
				workList.setTypeName(rs.getString("typeName"));
				workList.setItemName(rs.getString("itemName"));
				workList.setWebUrl(rs.getString("webUrl"));
				workLists.add(workList);
			}
		} catch (SQLException e) {
			wid.keepErrorMes(conn, ps, e,"", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {}
		}
		
		workListResult = new Gson().toJson(workLists);
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(workListResult);
	}

}

class WorkList {

	  private int typeSN;
	  private String typeName;
	  private String itemName;
	  private String webUrl;

	  
	  public int getTypeSN() {
		return typeSN;
	}

	public void setTypeSN(int typeSN) {
		this.typeSN = typeSN;
	}

	public String getTypeName()
	  {
	    return this.typeName;
	  }

	  public void setTypeName(String paramString)
	  {
	    this.typeName = paramString;
	  }

	  public String getItemName()
	  {
	    return this.itemName;
	  }

	  public void setItemName(String paramString)
	  {
	    this.itemName = paramString;
	  }

	  public String getWebUrl()
	  {
	    return this.webUrl;
	  }

	  public void setWebUrl(String paramString)
	  {
	    this.webUrl = paramString;
	  }

}